#include "morphologypallet.hpp"
#include <iostream>


MorphPallet::MorphPallet(){
    Glib::RefPtr<Gnome::Glade::Xml> Widgetstree = Gnome::Glade::Xml::create("interface3.glade");

    Widgetstree->get_widget("morphologypallet", window);
    Widgetstree->get_widget("combobox_struct_elem_type_erosion", elementtype_erosion);
    Widgetstree->get_widget("filechooserbutton_image_erosion", elementimage_erosion);
    Widgetstree->get_widget("spinbutton_centerx_erosion", centerxselector_erosion);
    Widgetstree->get_widget("spinbutton_centery_erosion", centeryselector_erosion);
    Widgetstree->get_widget("combobox_predefined_struct_erosion", predefinedtype_erosion);
    Widgetstree->get_widget("spinbutton_predefined_sizex_erosion", sizexselector_erosion);
    Widgetstree->get_widget("spinbutton_predefined_sizey_erosion", sizeyselector_erosion);
    Widgetstree->get_widget("hscale_iterations_erosion", iterationsselector_erosion);

    Widgetstree->get_widget("combobox_struct_elem_type1", elementtype_dilation);
    Widgetstree->get_widget("filechooserbutton_erosion_image1", elementimage_dilation);
    Widgetstree->get_widget("spinbutton_centerx1", centerxselector_dilation);
    Widgetstree->get_widget("spinbutton_centery1", centeryselector_dilation);
    Widgetstree->get_widget("combobox_predefined_struct1", predefinedtype_dilation);
    Widgetstree->get_widget("spinbutton_predefined_sizex1", sizexselector_dilation);
    Widgetstree->get_widget("spinbutton_predefined_sizey1", sizeyselector_dilation);
    Widgetstree->get_widget("hscale_iterations1", iterationsselector_dilation);

    Widgetstree->get_widget("combobox_struct_elem_type2", elementtype_open);
    Widgetstree->get_widget("filechooserbutton_erosion_image2", elementimage_open);
    Widgetstree->get_widget("spinbutton_centerx2", centerxselector_open);
    Widgetstree->get_widget("spinbutton_centery2", centeryselector_open);
    Widgetstree->get_widget("combobox_predefined_struct2", predefinedtype_open);
    Widgetstree->get_widget("spinbutton_predefined_sizex2", sizexselector_open);
    Widgetstree->get_widget("spinbutton_predefined_sizey2", sizeyselector_open);

    Widgetstree->get_widget("combobox_struct_elem_type3", elementtype_close);
    Widgetstree->get_widget("filechooserbutton_erosion_image3", elementimage_close);
    Widgetstree->get_widget("spinbutton_centerx3", centerxselector_close);
    Widgetstree->get_widget("spinbutton_centery3", centeryselector_close);
    Widgetstree->get_widget("combobox_predefined_struct3", predefinedtype_close);
    Widgetstree->get_widget("spinbutton_predefined_sizex3", sizexselector_close);
    Widgetstree->get_widget("spinbutton_predefined_sizey3", sizeyselector_close);

    Widgetstree->get_widget("prunningvalue", prunningvaluebutton);

    //Define o valor padrao dentre os itens da lista combo box
    elementtype_erosion->set_active(1);
    predefinedtype_erosion->set_active(0);

    elementtype_dilation->set_active(1);
    predefinedtype_dilation->set_active(0);

    elementtype_open->set_active(1);
    predefinedtype_open->set_active(0);

    elementtype_close->set_active(1);
    predefinedtype_close->set_active(0);

    prunningvalue=1;

    se_erosion=NULL;
    se_dilation=NULL;
    se_open=NULL;
    se_close=NULL;

    for(int j=0;j<4;j++){
        se_type[j]=PREDEFINED;
        centerx[j]=0;
        centery[j]=0;
        predefinedshape[j] = StructuringElement::DISK;
        sizex[j]=3;
        sizey[j]=3;
        iterations[j]=1;
    }


    //Conecta Sinais aos Callbacks
    elementtype_erosion->signal_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    elementimage_erosion->signal_selection_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    centerxselector_erosion->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    centeryselector_erosion->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    predefinedtype_erosion->signal_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    sizexselector_erosion->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    sizeyselector_erosion->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    iterationsselector_erosion->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));

    elementtype_dilation->signal_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    elementimage_dilation->signal_selection_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    centerxselector_dilation->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    centeryselector_dilation->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    predefinedtype_dilation->signal_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    sizexselector_dilation->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    sizeyselector_dilation->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    iterationsselector_dilation->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));

    elementtype_open->signal_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    elementimage_open->signal_selection_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    centerxselector_open->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    centeryselector_open->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    predefinedtype_open->signal_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    sizexselector_open->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    sizeyselector_open->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));

    elementtype_close->signal_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    elementimage_close->signal_selection_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    centerxselector_close->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    centeryselector_close->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    predefinedtype_close->signal_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    sizexselector_close->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));
    sizeyselector_close->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams));

    prunningvaluebutton->signal_value_changed().connect(sigc::mem_fun(*this, &MorphPallet::changeParams_2));

    //Exibe toda interface



    window->show_all();

}

Gtk::Window* MorphPallet::getWindow(){
    return window;
}

void MorphPallet::changeParams(){
    if(se_erosion!=NULL||se_dilation!=NULL||se_open!=NULL||se_close!=NULL)
        deleteStructElem();

    if(elementtype_erosion->get_active()==1)
        se_type[0]=PREDEFINED;
    else
        se_type[0]=FROMIMAGE;


    if(elementtype_dilation->get_active()==1)
        se_type[1]=PREDEFINED;
    else
        se_type[1]=FROMIMAGE;


    if(elementtype_open->get_active()==1)
        se_type[2]=PREDEFINED;
    else
        se_type[2]=FROMIMAGE;

    if(elementtype_open->get_active()==1)
        se_type[3]=PREDEFINED;
    else
        se_type[3]=FROMIMAGE;


    imagepath[0] = elementimage_erosion->get_filename();
    imagepath[1] = elementimage_dilation->get_filename();
    imagepath[2] = elementimage_open->get_filename();
    imagepath[3] = elementimage_close->get_filename();


    centerx[0] = centerxselector_erosion->get_value_as_int();
    centerx[1] = centerxselector_dilation->get_value_as_int();
    centerx[2] = centerxselector_open->get_value_as_int();
    centerx[3] = centerxselector_close->get_value_as_int();


    centery[0] = centeryselector_erosion->get_value_as_int();
    centery[1] = centeryselector_dilation->get_value_as_int();
    centery[2] = centeryselector_open->get_value_as_int();
    centery[3] = centeryselector_close->get_value_as_int();

    switch(predefinedtype_erosion->get_active()){
        case 0:
            predefinedshape[0]=StructuringElement::DISK;
            break;
        case 1:
            predefinedshape[0]=StructuringElement::CROSS1;
            break;
        case 2:
            predefinedshape[0]=StructuringElement::CROSS2;
            break;
        case 3:
            predefinedshape[0]=StructuringElement::RECTANGLE;
            break;
        default:
            predefinedshape[0]=StructuringElement::DISK;
            break;
    }
    switch(predefinedtype_dilation->get_active()){
        case 0:
            predefinedshape[1]=StructuringElement::DISK;
            break;
        case 1:
            predefinedshape[1]=StructuringElement::CROSS1;
            break;
        case 2:
            predefinedshape[1]=StructuringElement::CROSS2;
            break;
        case 3:
            predefinedshape[1]=StructuringElement::RECTANGLE;
            break;
        default:
            predefinedshape[1]=StructuringElement::DISK;
            break;
    }
    switch(predefinedtype_open->get_active()){
        case 0:
            predefinedshape[2]=StructuringElement::DISK;
            break;
        case 1:
            predefinedshape[2]=StructuringElement::CROSS1;
            break;
        case 2:
            predefinedshape[2]=StructuringElement::CROSS2;
            break;
        case 3:
            predefinedshape[2]=StructuringElement::RECTANGLE;
            break;
        default:
            predefinedshape[2]=StructuringElement::DISK;
            break;
    }
    switch(predefinedtype_close->get_active()){
        case 0:
            predefinedshape[3]=StructuringElement::DISK;
            break;
        case 1:
            predefinedshape[3]=StructuringElement::CROSS1;
            break;
        case 2:
            predefinedshape[3]=StructuringElement::CROSS2;
            break;
        case 3:
            predefinedshape[3]=StructuringElement::RECTANGLE;
            break;
        default:
            predefinedshape[3]=StructuringElement::DISK;
            break;
    }

    sizex[0]=sizexselector_erosion->get_value_as_int();
    sizex[1]=sizexselector_dilation->get_value_as_int();
    sizex[2]=sizexselector_open->get_value_as_int();
    sizex[3]=sizexselector_close->get_value_as_int();

    sizey[0]=sizeyselector_erosion->get_value_as_int();
    sizey[1]=sizeyselector_dilation->get_value_as_int();
    sizey[2]=sizeyselector_open->get_value_as_int();
    sizey[3]=sizeyselector_close->get_value_as_int();

    iterations[0]=iterationsselector_erosion->get_value();
    iterations[1]=iterationsselector_dilation->get_value();

}
void MorphPallet::createStructElem(){
    if(se_type[0]==PREDEFINED)
        se_erosion = new StructuringElement(sizey[0],sizex[0],Position(sizex[0]/2,sizey[0]/2),predefinedshape[0]);
    if(se_type[1]==PREDEFINED)
        se_dilation = new StructuringElement(sizey[1],sizex[1],Position(sizex[1]/2,sizey[1]/2),predefinedshape[1]);
    if(se_type[2]==PREDEFINED)
        se_open = new StructuringElement(sizey[2],sizex[2],Position(sizex[2]/2,sizey[2]/2),predefinedshape[2]);
    if(se_type[3]==PREDEFINED)
        se_close = new StructuringElement(sizey[3],sizex[3],Position(sizex[3]/2,sizey[3]/2),predefinedshape[3]);
}

void MorphPallet::deleteStructElem(){
    if(se_erosion!=NULL)
        delete se_erosion;
    if(se_dilation!=NULL)
        delete se_dilation;
    if(se_open!=NULL)
        delete se_open;
    if(se_close!=NULL)
        delete se_close;
    se_erosion=NULL;
    se_dilation=NULL;
    se_open=NULL;
    se_close=NULL;
}

void MorphPallet::changeParams_2(){
    prunningvalue=prunningvaluebutton->get_value_as_int();
}
